package com.wanghua.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghua.common.dto.SkuReductionDto;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.Query;
import com.wanghua.mall.coupon.dao.SkuFullReductionDao;
import com.wanghua.mall.coupon.entity.SkuFullReductionEntity;
import com.wanghua.mall.coupon.entity.SkuLadderEntity;
import com.wanghua.mall.coupon.entity.UserPriceEntity;
import com.wanghua.mall.coupon.service.SkuFullReductionService;
import com.wanghua.mall.coupon.service.SkuLadderService;
import com.wanghua.mall.coupon.service.UserPriceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private UserPriceService userPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 供product调用，保存sku的满减、优惠信息
     */
    @Transactional
    @Override
    public void saveSkuReductionDto(SkuReductionDto skuReductionDto) {
        //1、打折
        if (skuReductionDto.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionDto, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionDto.getCountStatus());
            skuLadderEntity.setPrice(new BigDecimal(0));//折后价
            skuLadderService.save(skuLadderEntity);
        }

        //2、满减
        if (skuReductionDto.getFullCount() > 0) {
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionDto, skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(skuReductionDto.getPriceStatus());
            baseMapper.insert(skuFullReductionEntity);
        }

        //3、会员价格
        List<SkuReductionDto.UserPrice> userPriceList = skuReductionDto.getUserPrice();
        if (userPriceList.size() != 0) {
            List<UserPriceEntity> userPriceEntityList = userPriceList.stream().map((item) -> {
                UserPriceEntity userPriceEntity = new UserPriceEntity();
                userPriceEntity.setSkuId(skuReductionDto.getSkuId());
                userPriceEntity.setUserLevelId(item.getId());
                userPriceEntity.setUserLevelName(item.getName());
                userPriceEntity.setUserPrice(item.getPrice());
                userPriceEntity.setAddOther(1);
                return userPriceEntity;
            }).filter((item) -> item.getUserPrice().compareTo(new BigDecimal(0)) > 0).collect(Collectors.toList());
            userPriceService.saveBatch(userPriceEntityList);
        }


    }

}